﻿Imports Lucene.Net.Analysis.Standard
Imports Lucene.Net.Documents
Imports Lucene.Net.Store
Imports Directory = Lucene.Net.Store.Directory
Imports Lucene.Net.Index
Imports Lucene.Net.Search
Imports Lucene.Net.QueryParsers



Public Class createindex
    Inherits System.Web.UI.Page


    Protected Sub btnCreateIndex_Click(sender As Object, e As EventArgs) Handles btnCreateIndex.Click
        Dim cnn As New MySql.Data.MySqlClient.MySqlConnection

        Me.ListBox1.Items.Clear()
        Me.ListBox1.Items.Add("Connecting to database ...")

        cnn.ConnectionString = ConfigurationManager.ConnectionStrings("ads_dbConnectionString").ConnectionString
        cnn.Open()
        Me.ListBox1.Items.Add("Connection Succeed!")
        Me.ListBox1.Items.Add("Creating command")
        Dim cmd As New MySql.Data.MySqlClient.MySqlCommand
        cmd.Connection = cnn
        cmd.CommandText = "Select *  from " & Me.TextBox2.Text

        Dim dr As MySql.Data.MySqlClient.MySqlDataReader
        dr = cmd.ExecuteReader
        Me.ListBox1.Items.Add("Execute command and create index")

        Dim mydir As Directory = FSDirectory.Open(New System.IO.DirectoryInfo(Server.MapPath("\indexfolder")))
        Dim analyz As New StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29)
        Dim writer As New IndexWriter(mydir, analyz, True, IndexWriter.MaxFieldLength.LIMITED)

        Do While dr.Read
            'Me.ListBox1.Items.Add(dr(0).ToString)
            'TODO: Read each row and add it to the index
            Dim myDoc As New Lucene.Net.Documents.Document
            myDoc.Add(New Field("AdID", dr(1).ToString, Field.Store.YES, Field.Index.NOT_ANALYZED))
            myDoc.Add(New Field("AdText", dr(2).ToString, Field.Store.YES, Field.Index.ANALYZED))
            writer.AddDocument(myDoc)
        Loop
        writer.Optimize()
        writer.Dispose()
        dr.Close()
        cnn.Close()
        Me.ListBox1.Items.Add("Index Created at " & Server.MapPath("\indexfolder"))
        Me.lblCreateIndexOutput.Text = "Index Created at " & Server.MapPath("\indexfolder")
    End Sub


    Private Sub createindex_Load(sender As Object, e As EventArgs) Handles Me.Load
        Me.TextBox1.Text = Session("myConnection")
    End Sub
End Class